package cn.wolfcode.wolf2w.listener;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.redis.IRedisStatisService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

//@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IRedisStatisService statisService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.out.println("------------start--------------");
        //把数据库中的数据设置到缓存中
        List<Strategy> strategies = strategyService.list();
        for (Strategy strategy : strategies) {
            if(statisService.isExistVo(strategy.getId())){
                continue;
            }
            StrategyStatisVO statisVO = new StrategyStatisVO();
            BeanUtils.copyProperties(strategy,statisVO);
            statisVO.setStrategyId(strategy.getId());
            statisService.setStatisVo(statisVO);
        }
        System.out.println("------------end--------------");
    }
}
